const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}
//返回樓價所屬區間最大比例
function JudgementHoust(houseprice, MCinmate, Purchasing, Moyouth, calculateType) {
  var price = parseFloat(houseprice)
  var p = 0;

   
  if (calculateType == 'Real') //現樓
    p = GetEsseHouse(price, MCinmate, Purchasing, Moyouth)
  if (calculateType == 'Stage')
    p = GetStageHouse(price, MCinmate, Purchasing, Moyouth)
  return p;
}
///实楼按揭
function GetEsseHouse(price, MCinmate, Purchasing, Moyouth) {
  //debugger
  var p = 0;
  //>800万
  if (price > 8000000) {
    if (MCinmate == 'MC1')//澳門居民
    {
      if (Purchasing == 'first') {
        p = 0.5;

      }
      if (Purchasing == 'nofirst')
        p = 0.4
    }
    if (MCinmate == 'MC2')//非澳門居民
    {
      p = 0.3
    }
  }
  if (price > 6000000 && price <= 8000000) {
    if (MCinmate == 'MC1')//澳門居民
    {
      if (Purchasing == 'first')//首次置業
      {
        p = 0.6;
        if (Moyouth == 'youth') //澳门青年首次置业
          p = 0.8;
      }
      if (Purchasing == 'nofirst')
        p = 0.5
    }
    if (MCinmate == 'MC2')//非澳門居民
    {
      p = 0.4
    }
  }
  if (price > 3300000 && price <= 6000000) {
    if (MCinmate == 'MC1')//澳門居民
    {
      if (Purchasing == 'first')//首次置業
      {
        p = 0.7;
        if (Moyouth == 'youth') //澳门青年首次置业
          p = 0.8;
      }
      if (Purchasing == 'nofirst')
        p = 0.6
    }
    if (MCinmate == 'MC2')//非澳門居民
    {
      p = 0.5
    }
  }
  if (price > 0 && price <= 3300000) {
    if (MCinmate == 'MC1')//澳門居民
    {
      if (Purchasing == 'first')//首次置業
        p = 0.9
      if (Purchasing == 'nofirst')
        p = 0.7
    }
    if (MCinmate == 'MC2')//非澳門居民
    {
      p = 0.6
    }
  }
  return p;
}
//楼花计算
function GetStageHouse(price, MCinmate, Purchasing, Moyouth) {
  
  var p = 0;
  //>800万
  if (price > 8000000) {
    if (MCinmate == 'MC1')//澳門居民
    {
      if (Purchasing == 'first') {
        p = 0.5;
        // if (Moyouth =='youth') //澳门青年首次置业
        //     p = 0.8;
      }
      if (Purchasing == 'nofirst')
        p = 0.4
    }
    if (MCinmate == 'MC2')//非澳門居民
    {
      p = 0.3
    }
  }
  if (price > 6000000 && price <= 8000000) {
    if (MCinmate == 'MC1')//澳門居民
    {
      if (Purchasing == 'first')//首次置業
      {
        p = 0.6;
        if (Moyouth == 'youth') //澳门青年首次置业
          p = 0.8;
      }
      if (Purchasing == 'nofirst')
        p = 0.5
    }
    if (MCinmate == 'MC2')//非澳門居民
    {
      p = 0.4
    }
  }
  if (price <= 6000000) {
    if (MCinmate == 'MC1')//澳門居民
    {
      if (Purchasing == 'first')//首次置業
      {
        p = 0.6;
        if (Moyouth == 'youth') //澳门青年首次置业
          p = 0.8;
      }
      if (Purchasing == 'nofirst')
        p = 0.6
    }
    if (MCinmate == 'MC2')//非澳門居民
    {
      p = 0.4
    }
  }
  return p;
}
//贷款上限
function GetMaxLoan(mortgage, MCinmate, Purchasing, Moyouth,calculateType ){
  
  
   if (calculateType == 'Real') //現樓
     return GetEsseLoad(parseFloat(mortgage), MCinmate, Purchasing, Moyouth) ;
}
//实楼按揭金额上限 //无就系0
function GetEsseLoad(price, MCinmate, Purchasing, Moyouth){
  var p=0;
  if (price > 8000000) {
    if (MCinmate == 'MC1')//澳門居民
    {
      if (Purchasing == 'first') {
        p = 0;

      }
      if (Purchasing == 'nofirst')
        p = 0
    }
    if (MCinmate == 'MC2')//非澳門居民
    {
      p = 0
    }
  }
  if (price > 6000000 && price <= 8000000) {
    if (MCinmate == 'MC1')//澳門居民
    {
      if (Purchasing == 'first')//首次置業
      {
          p = 4000000;
        if (Moyouth == 'youth') //澳门青年首次置业
          p = 6400000;
      }
      if (Purchasing == 'nofirst')
        p = 3200000
    }
    if (MCinmate == 'MC2')//非澳門居民
    {
      p = 2400000
    }
  }
  if (price > 3300000 && price <= 6000000) {
    if (MCinmate == 'MC1')//澳門居民
    {
      if (Purchasing == 'first')//首次置業
      {
        p = 3600000;
        if (Moyouth == 'youth') //澳门青年首次置业
          p = 6400000;
      }
      if (Purchasing == 'nofirst')
        p = 3000000
    }
    if (MCinmate == 'MC2')//非澳門居民
    {
      p = 2400000
    }
  }
  if (price > 0 && price <= 3300000) {
    if (MCinmate == 'MC1')//澳門居民
    {
      if (Purchasing == 'first')//首次置業
      {
        p = 2310000;
        if (Moyouth == 'youth') //澳门青年首次置业
          p = 2640000;
       }
      if (Purchasing == 'nofirst')
        p = 1980000;
    }
    if (MCinmate == 'MC2')//非澳門居民
    {
      p = 1650000;
    }
  }
  return p;
}


function replaceNum(num) {
  return num.replace(/,/g, "");
}
//限制輸入小數位數
//num 數字
//digits 精度
function Numberdigits(num, digits) {
  var srate = num.toString().split(".")[1];
  if (srate != undefined) {
    if (srate.length > digits) {
      var newrate = num.toString().substring(0, num.toString().length - (srate.length - digits))
      return newrate
    }
  }
}
//千分位符號
function formatCurrency(num) {



  if (num) {
    //将num中的$,去掉，将num变成一个纯粹的数据格式字符串
    num = num.toString().replace(/\$|\,/g, '');
    //如果num不是数字，则将num置0，并返回
    if ('' == num || isNaN(num)) { return 'Not a Number ! '; }
    //如果num是负数，则获取她的符号
    var sign = num.indexOf("-") > 0 ? '-' : '';
    //如果存在小数点，则获取数字的小数部分
    var cents = num.indexOf(".") > 0 ? num.substr(num.indexOf(".")) : '';
    cents = cents.length > 1 ? cents : '';//注意：这里如果是使用change方法不断的调用，小数是输入不了的
    //获取数字的整数数部分
    num = num.indexOf(".") > 0 ? num.substring(0, (num.indexOf("."))) : num;
    //如果没有小数点，整数部分不能以0开头
    if ('' == cents) { if (num.length > 1 && '0' == num.substr(0, 1)) { return 'Not a Number ! '; } }
    //如果有小数点，且整数的部分的长度大于1，则整数部分不能以0开头
    else { if (num.length > 1 && '0' == num.substr(0, 1)) { return 'Not a Number ! '; } }
    //针对整数部分进行格式化处理，这是此方法的核心，也是稍难理解的一个地方，逆向的来思考或者采用简单的事例来实现就容易多了

    //  也可以这样想象，现在有一串数字字符串在你面前，如果让你给他家千分位的逗号的话，你是怎么来思考和操作的?
    // 字符串长度为0/1/2/3时都不用添加
    //  字符串长度大于3的时候，从右往左数，有三位字符就加一个逗号，然后继续往前数，直到不到往前数少于三位字符为止
    //
    for (var i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++) {
      num = num.substring(0, num.length - (4 * i + 3)) + ',' + num.substring(num.length - (4 * i + 3));
    }
    //将数据（符号、整数部分、小数部分）整体组合返回
    return (sign + num + cents);
  }

}



module.exports = {
  formatTime: formatTime,
  JudgementHoust: JudgementHoust,
  formatCurrency: formatCurrency,
  replaceNum: replaceNum,
  Numberdigits: Numberdigits,
  GetMaxLoan: GetMaxLoan
}
